library(tidyverse)
library(janitor)
library(readxl)

Reading in the data

The Excel file read in this example is analytic_data.xlxs. Replace this with your Excel file.

In this R Markdown file, the data frame is called EXAMPLE_DATA. Replace this with the name of the file you wish to use.

EXAMPLE_DATA <- read_excel("analytic_data.xlsx")
EXAMPLE_DATA <- EXAMPLE_DATA %>% 
  mutate_if(is.character,as.factor)

In all of the code below, you will need to replace EXAMPLE_DATA with the name of your data frame. You will need to use the appropriate variable names.

Bar chart

ggplot(EXAMPLE_DATA, aes(y=CATEGORICAL_VARIABLE2)) + 
  geom_bar(width = 0.5) +
  labs(y="NICE AXIS LABEL", x = "Frequency")

Scatterplot

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2)) +
  geom_point() +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 

Add a smoother.

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2)) +
  geom_point() +
  geom_smooth(se = FALSE) +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Add a linear regression line.

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2)) +
  geom_point() +
  geom_smooth(method=lm, se=FALSE, fullrange=TRUE) +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 
## `geom_smooth()` using formula = 'y ~ x'

Add groups

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1, y = NUMERICAL_VARIABLE2, colour=CATEGORICAL_VARIABLE1)) +
  geom_point() +
  labs(x="NICE X-AXIS LABEL", y = "NICE Y-AXIS LABEL") 

Histogram

You can edit the number of bins, using ’bins =“; between 5 and 15 can be useful.

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1)) +
      geom_histogram(bins = 15) +
      labs(x="NICE AXIS LABEL") 

Boxplot

ggplot(EXAMPLE_DATA, aes(x=NUMERICAL_VARIABLE1)) + 
      geom_boxplot() + 
      labs(x="NICE AXIS LABEL") +
      scale_y_continuous(breaks=NULL) 

Time series plot

ggplot(EXAMPLE_DATA, aes(x=TIME_VARIABLE, y = NUMERICAL_VARIABLE1)) +
  geom_point() +
  geom_line() +
  labs(x="Time", y = "NICE Y-AXIS LABEL")

Dot chart / lollypop

freqTable <- EXAMPLE_DATA %>%
  tabyl(CATEGORICAL_VARIABLE2)

ggplot(freqTable, aes(x = n, y = CATEGORICAL_VARIABLE2)) + 
  geom_point(size=3) +
  geom_segment(aes(x=0, 
                   xend=n, 
                   y=CATEGORICAL_VARIABLE2, 
                   yend=CATEGORICAL_VARIABLE2)) +
  labs(x = "Frequency", y = "NICE AXIS LABEL")

Adding a categorical grouping to a plot of a numerical variable

ggplot(EXAMPLE_DATA, aes(y = CATEGORICAL_VARIABLE1, x = NUMERICAL_VARIABLE1)) + 
        geom_boxplot() +
        labs(y ="NICE Y-AXIS LABEL", x = "NICE X-AXIS LABEL")

Using facet_wrap for additional categorical grouping

ggplot(EXAMPLE_DATA, aes(y = CATEGORICAL_VARIABLE1, x = NUMERICAL_VARIABLE1)) + 
        geom_boxplot() +
        labs(y ="NICE Y-AXIS LABEL", x = "NICE X-AXIS LABEL") +
  facet_wrap(vars(CATEGORICAL_VARIABLE2), nrow=2)

© Statistical Consulting Centre, University of Melbourne, 2023